<style scoped>
  .version-form {
    width: 500px;
    margin: 100px auto auto;
    background-color: #fdfdfd;
    padding-top: 20px;
    padding-right: 50px;
    padding-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }
</style>
<template>
  <div class="mod-config">
    <div class="version-form">
      <el-form :inline="false" :rules="dataRules" :model="versionData" label-width="100px" ref="version-form">
        <el-form-item label="版本号：" prop="versionName">
          <el-input v-model="versionData.versionName" placeholder="版本号"></el-input>
        </el-form-item>
        <el-form-item label="版本标题：" prop="title">
          <el-input v-model="versionData.title" placeholder="版本标题"></el-input>
        </el-form-item>
        <el-form-item label="更新内容：" prop="content">
          <el-input v-model="versionData.content" placeholder="版本更新内容"></el-input>
        </el-form-item>
        <el-form-item label="下载地址：" prop="downloadUrl">
          <el-input v-model="versionData.downloadUrl" placeholder="下载地址"></el-input>
        </el-form-item>
        <el-form-item label="更新时间：">
           <span v-text="versionData.updateTime"></span>
        </el-form-item>
        <el-form-item label="强制更新：">
          <el-switch v-model="forceUpdate"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitVersionData()">提交</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>

  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        versionData: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: true,
        dataListSelections: [],
        addOrUpdateVisible: false,
        forceUpdate:false,
        dataRules:{
          versionName: [
            { required: true, message: '请输入版本号', trigger: 'blur' }
          ],
          downloadUrl: [
            { required: true, message: '请输入下载地址', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '请输入版本描述信息', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
        },
      }
    },
    components: {
     // AddOrUpdate
    },
    created() {
      this.getNewestAndroidVersion()
    },
    methods: {
      getNewestAndroidVersion() {
        this.$http({
          url: this.$http.adornUrl('/simida/version/info'),
          method: 'get',
        }).then(({data}) => {

          if (data && data.code === 0) {
            if (data.version){
               this.forceUpdate = (data.version.forceUpdateState == 1);
               this.versionData = data.version;
            }
          } else {
            this.$message.error(data.msg);
          }
        })
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/simida/version/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = [];
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      submitVersionData(){
        this.$refs['version-form'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/simida/version/${!this.versionData.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.versionData.id || undefined,
                'versionName': this.versionData.versionName,
                'downloadUrl': this.versionData.downloadUrl,
                'forceUpdateState': this.forceUpdate ? 1:0,
                'content': this.versionData.content,
                'title': this.versionData.title,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.getNewestAndroidVersion()
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
